$(function () {

  function disableSubmitButtons(on) {
    $('#form').data('formValidation').disableSubmitButtons(on);
  }

  $('#form')
      .formValidation({
        message: 'This value is not valid',
        icon: {
          valid: 'glyphicon glyphicon-ok',
          invalid: 'glyphicon glyphicon-remove',
          validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
          email: {
            validators: {
              notEmpty: {
                message: gettext('Your email address is required and can\'t be empty')
              }
            }
          }
        }
      })
      .on('success.field.fv', function (e, data) {
        disableSubmitButtons(data.fv.getInvalidFields().length > 0);
      })
      .on('success.form.fv', function (e) {
        e.preventDefault();
        $.requestJSON({
            url: Urls.resolve('accounts_api_v1:reset_password_request'),
            method: "POST",
            data: {'email': $("#email").val()},
            onSuccess: function (j) {
              $.showModal({
                title: ' ',
                message: j.data,
                onHide: function () {
                  window.location = Urls.resolve('site_index');
                }
              });
            },
            onError: function () {
              disableSubmitButtons(false);
            }
        });

      });

});

